"""
Case Type   : 资源池化
Case Name   : 主备倒换后，缩容旧主机
Create At   : 2024.10
Owner       : chen
Description :
    1.主备倒换 
    cm_ctl switchover -A
    2.新主机上执行缩容，缩容旧主机
    gs_dropnode -U USER -G GROUP -h hostlist
    3.扩容旧主机，恢复环境
Expect      :
    1.成功
    2.成功
    3.恢复成功
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.sta1_node = Node('Standby1DbUser')
        self.sta1_root = Node('Standby1Root')
        self.db_host = self.pri_node.db_host
        self.s_user = self.pri_node.ssh_user

    def test_shared_storage(self):
        text = f'''-----step1: 主备倒换 expect: 成功-----'''
        self.log.info(text)
        res = self.pri_comsh.exec_cm_ctl('switchover -A')
        self.log.info(res)
        self.assertIn(self.constant.cm_switchover_A_success_msg, res)

        text = f'''-----step2: 新主机上执行缩容，缩容旧主机 expect: 成功-----'''
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 600
        spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host}
        expect "*drop the target node (yes/no)?*"
        send "yes\\n"
        expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.sta1_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Success to drop the target nodes', res)
        cmd = f'source {macro.DB_ENV_PATH}; gs_uninstall --delete-data'
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)

    def tearDown(self):
        text = f'''-----step3: 恢复环境 expect: 成功-----'''
        self.log.info(text)
        self.log.info('----执行扩容----')
        xml_path = macro.DB_XML_PATH
        cmd = f'''gs_expansion -U {self.s_user} -G \
            {self.s_user} -X {xml_path} -h {self.db_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 300
        spawn {cmd}
        expect "*assword*:"
        send "{self.sta1_root.ssh_password}\\n"
        expect "*assword*:"
        send "{self.sta1_root.ssh_password}\\n"
        expect "*lease repeat for database*:"
        send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
        expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.sta1_root.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)
        self.log.info('----恢复集群----')
        res = self.pri_comsh.exec_cm_ctl('switchover -a')
        self.assertIn(self.constant.cm_switchover_success_msg, res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')